<?php
namespace App\Http\Controllers;

use App\Http\Logic\UtilLogic;
use App\Http\Requests\Util\GetIdCardInfoRequest;
use App\Http\Requests\Util\QueryCountriesrRequest;
use App\Http\Requests\Util\SendEmailCodeRequest;
use App\Http\Requests\Util\SendSmsCodeRequest;
use App\Http\Requests\Util\UploadFileRequest;

class UtilController extends BaseController
{
    public function queryCountries(QueryCountriesrRequest $request)
    {
        $list = UtilLogic::queryCountries();

        return $this->success($list);
    }

    public function sendSmsCode(SendSmsCodeRequest $request)
    {
        $phone = $request->get('phone');

        $type = $request->get('type');

        $result = UtilLogic::sendSmsCode($phone, $type);

        if(false === $result) {
            return $this->error(UtilLogic::getErrorMessage());
        }

        return $this->success();
    }

    public function sendEmailCode(SendEmailCodeRequest $request)
    {
        $email = $request->get('email');

        $type = $request->get('type');

        $result = UtilLogic::sendEmailCode($email, $type);

        if(false === $result) {
            return $this->error(UtilLogic::getErrorMessage());
        }

        return $this->success();
    }

    public function uploadFile(UploadFileRequest $request)
    {
        $file = $request->file('file');

        $result = UtilLogic::upload($file);

        if(false === $result) {
            return $this->error(UtilLogic::getErrorMessage());
        }

        return $this->success($result);
    }

    public function analysisIdCard(GetIdCardInfoRequest $request)
    {
        $type = $request->input('type');

        $url = $request->input('url');

        $result = UtilLogic::analysisIdCard($type, $url);

        if(false === $result) {
            return $this->error(UtilLogic::getErrorMessage());
        }

        return $this->success($result);
    }

    public function getAppVersion()
    {
        return $this->success([
            'version'          => env('APP_VERSION'),
            'android_download' => config('app.url').'/swtb.apk',
            'force_update'     => env('APP_FORCE_UPDATE'),
            "version_content"  => ""
        ]);
    }
}
